package com.fdb.efp.limit.service.common;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Objects;

import com.fdb.efp.limit.service.vo.PreListAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fdb.efp.console.service.vo.PrdRateStdVO;
import com.fdb.efp.ctr.service.vo.CtrLoanContVO;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.exception.PauseException;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.limit.common.LimitConstant;
import com.fdb.efp.limit.service.domain.LmtPrelist;
import com.fdb.efp.limit.service.vo.PreCreStatus;
import com.fdb.efp.limit.service.vo.PreListAppListVO;

/**
 * 预授信状态、总额度、可激活额度判断逻辑
 * @author lishaotang
 * @since 2018-05-07
 *
 */
public class PreListAppUtil {
	
	private static Logger logger = LoggerFactory.getLogger(PreListAppUtil.class);
	
	/**
	 * 基本信息从预授信表VO对象转换到手机银行对应vo对象
	 * @param preListAppVO
	 * @param lmtPrelist
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static PreListAppVO lmtVO2AppVO(
			PreListAppVO preListAppVO,
			LmtPrelist lmtPrelist) throws UnsupportedEncodingException {
		logger.debug("当前装换参数为:");
		preListAppVO.setCdtSerno(lmtPrelist.getPreSerno());//预授信编号
		preListAppVO.setCdtQuota(lmtPrelist.getActLmtAmt());//授信额度
		preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());//可激活额度
		preListAppVO.setCertCode(lmtPrelist.getCertCode());//证件号码
		preListAppVO.setCreditType(lmtPrelist.getLmtType());//授信类型
		//preListAppVO.setPreCreStatus(lmtPrelist.getStatus());//预授信状态
		preListAppVO.setCusId(lmtPrelist.getCusId());//客户编号
		preListAppVO.setCusName(lmtPrelist.getCusName());//客户名称
		preListAppVO.setPreTerm(lmtPrelist.getActValidDays().toString());//预授信期限
		preListAppVO.setTermUnit(lmtPrelist.getTermType());//期限单位类型
		preListAppVO.setIsAllowMent(null);//是否允许提额
		//preListAppVO.setIsExistCont("32".equals(lmtPrelist.getStatus())?"1":"2");//是否有合同 1有 2没有
		preListAppVO.setIsExistCont(BaseConstant.NO);//是否有合同 1有 2没有
		preListAppVO.setPrdId(lmtPrelist.getPrdId());//产品编号
		//preListAppVO.setLoanAcctNo("32".equals(lmtPrelist.getStatus())?"30010000025036190":null);//贷款账号
		preListAppVO.setList(null);//贷款账号
		DecimalFormat df = new DecimalFormat("00.0000000");
		if (Objects.nonNull(lmtPrelist.getFixedRate())) {
			String rate = df.format(lmtPrelist.getFixedRate().doubleValue());
			if (Objects.nonNull(lmtPrelist.getFixedRate())) {
				preListAppVO.setRealityIrY(new BigDecimal(rate.substring(0, rate.length()-1)));//年利率（固定利率值）
			}			
		}
		preListAppVO.setMngBrId(lmtPrelist.getLegalOrgCode());//法人机构号
		preListAppVO.setMngBrName(lmtPrelist.getLegalOrgName());//法人机构名称
		//preListAppVO.setOrgShortName(new String("佛山农商行".getBytes("UTF-8")));
		String brchShrtName = lmtPrelist.getLegalOrgSimpleName();
		preListAppVO.setOrgShortName(brchShrtName==null?lmtPrelist.getLegalOrgName():brchShrtName);
		return preListAppVO;
	}
	
	
	/**
	 * 预授信状态校验
	 * @param preListAppVO
	 * @param lmtPrelist
	 * @return
	 */
	public static PreListAppVO preCreStatus_Estimate(
			PreListAppVO preListAppVO,
			LmtPrelist lmtPrelist)
	{	
		if (LimitConstant.PRELMT_TYPE_2.equals(lmtPrelist.getLmtType())) {//营销额度
			if (LimitConstant.PRELMT_STS_10.equals(lmtPrelist.getStatus())) {//待申请
				switch (lmtPrelist.getCurOp()) {
				case LimitConstant.PRELMT_OP_00://额度类型为营销额度&状态为待申请&当前操作为无
					preListAppVO.setPreCreStatus(PreCreStatus.APPLY_WAIT.getCode());
					preListAppVO.setActCdt(new BigDecimal("0"));
					preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
				    break;
				case LimitConstant.PRELMT_OP_10://额度类型为营销额度&状态为待申请&当前操作为额度申请中
					preListAppVO.setPreCreStatus(PreCreStatus.APPLY_DO.getCode());
					preListAppVO.setActCdt(new BigDecimal("0"));//可激活额度
					preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());//授信额度
					break;
				}
				return preListAppVO;
			}else if (LimitConstant.PRELMT_STS_30.equals(lmtPrelist.getStatus())&&LimitConstant.PRELMT_OP_00.equals(lmtPrelist.getCurOp())) {
				//额度类型为营销额度&状态为待激活&当前操作为无
				preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_WAIT.getCode());
				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
				preListAppVO.setCreditType(LimitConstant.PRELMT_TYPE_1);
				return preListAppVO;
			}
		}else if (LimitConstant.PRELMT_TYPE_1.equals(lmtPrelist.getLmtType())) {//预授信额度
			if (LimitConstant.PRELMT_STS_30.equals(lmtPrelist.getStatus())//待激活
					&&LimitConstant.PRELMT_OP_00.equals(lmtPrelist.getCurOp())) {
				//额度类型为预授信额度&状态为待激活&当前操作为无 
				preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_WAIT.getCode());
				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
				return preListAppVO;
			}
		}
		
		/**
		 * 激活申请
		 */
		if (LimitConstant.PRELMT_STS_30.equals(lmtPrelist.getStatus())) {
			//状态为待激活&当前操作为激活中
//			if ("激活中".equals(lmtPrelist.getCurOp())) {
//				preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
//				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_DO.getCode());
//				preListAppVO.setActCdt(lmtPrelist.getLmtAmt());
//			return preListAppVO;	
//			}
			preListAppVO.setCreditType(LimitConstant.PRELMT_TYPE_1);
			switch (lmtPrelist.getCurOp()) {
			case LimitConstant.PRELMT_OP_20://状态为待激活&当前操作为激活中
				preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_DO.getCode());
				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
				break;
			case LimitConstant.PRELMT_OP_30://状态为待激活&当前操作为提额中
				preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
				preListAppVO.setPreCreStatus(PreCreStatus.MODIFY_DO.getCode());
				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
				break;
			case LimitConstant.PRELMT_OP_31://状态为待激活&当前操作为提额成功
				preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
				preListAppVO.setPreCreStatus(PreCreStatus.MODIFY_FIN.getCode());
				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
				break;
			}		
		}else if (LimitConstant.PRELMT_STS_32.equals(lmtPrelist.getStatus())) {//已激活
			preListAppVO.setCreditType(LimitConstant.PRELMT_TYPE_1);
			switch (lmtPrelist.getCurOp()) {
			case LimitConstant.PRELMT_OP_00://状态为已激活&当前操作为无
				preListAppVO.setCdtQuota(lmtPrelist.getActLmtAmt());
				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_ON.getCode());
				preListAppVO.setActCdt(new BigDecimal("0"));
				
				break;
//			case "提额中"://状态为已激活&当前操作为提额中
//				preListAppVO.setCdtQuota(lmtPrelist.getActLmtAmt());
//				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_DO.getCode());
//				preListAppVO.setActCdt(new BigDecimal("0"));
//				break;
//			case "提额完成"://状态为已激活&当前操作为提额完成	
//				preListAppVO.setCdtQuota(lmtPrelist.getActLmtAmt());
//				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_DO.getCode());
//				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
//				break;
//			case "激活中"://状态为已激活&当前操作为激活中	
//				preListAppVO.setCdtQuota(lmtPrelist.getActLmtAmt());
//				preListAppVO.setPreCreStatus(PreCreStatus.ACTIVE_DO.getCode());
//				preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
//				break;
			}
			return preListAppVO;			
		}else if(LimitConstant.PRELMT_STS_97.equals(lmtPrelist.getStatus())){
			preListAppVO.setCreditType(LimitConstant.PRELMT_TYPE_1);
			preListAppVO.setCdtQuota(lmtPrelist.getLmtAmt());
			preListAppVO.setPreCreStatus(PreCreStatus.OPEN_ACCONUT_FAILED.getCode());
			preListAppVO.setActCdt(lmtPrelist.getWaitActAmt());
			return preListAppVO;		
		}else if (LimitConstant.PRELMT_STS_90.equals(lmtPrelist.getStatus())) {
			preListAppVO.setCreditType(LimitConstant.PRELMT_TYPE_2);
			preListAppVO.setPreCreStatus(LimitConstant.PRELMT_STS_FAIL);
		}
		return preListAppVO;
	}
	
	/**
	 * 通过产品编号查询产品表_组装查询产品表对应的VO对象
	 * @param lmtPrelist
	 * @return
	 */
	public static PrdRateStdVO getPrdInfoId(
			LmtPrelist lmtPrelist) {
		String prdId = lmtPrelist.getPrdId();
		PrdRateStdVO prdRateStdVO = new PrdRateStdVO();
		prdRateStdVO.setRateTypeName("10");
		prdRateStdVO.setTerm(lmtPrelist.getTerm().intValue());
		return prdRateStdVO;
	}
	
	
	/**
	 * 通过授信协议编号查询是否有贷款和贷款账号_组装贷款账号查询VO
	 * @param preListAppVO
	 * @param lmtPrelist
	 * @return
	 */
	public static CtrLoanContVO getCtrLoanCont(
			PreListAppVO preListAppVO,
			LmtPrelist lmtPrelist){
		CtrLoanContVO ctrLoanContVO = new CtrLoanContVO();
		ctrLoanContVO.setSerno(lmtPrelist.getLmtSerno());
		ctrLoanContVO.setLmtContNo(lmtPrelist.getLmtContNo());
		return ctrLoanContVO;
	}
	
	
	/**
	 * 插入浮动年利率
	 * @param preListAppVO
	 * @param lmtPrelist
	 * @param prdInfoVO
	 * @return
	 * @throws PauseException 
	 */
	public static PreListAppVO setRealityIrY(PreListAppVO preListAppVO,
			LmtPrelist lmtPrelist, BigDecimal baseRate) throws PauseException {
		if (Objects.isNull(baseRate)) {
			throw  new PauseException("查询基准利率为空");
		}
		String irFloatType = lmtPrelist.getIrFloatType();//浮动方式
		BigDecimal irFloatPct = lmtPrelist.getIrFloatPct();//浮动比例
		DecimalFormat df = new DecimalFormat("00.0000000");
		BigDecimal rateY = null;
		if (StringUtil.isNotEmpty(irFloatType)&&Objects.nonNull(irFloatPct)) {
			switch (irFloatType) {
			case BaseConstant.IR_FLOAT_TYPE_UP://基准上浮
				rateY = baseRate.multiply(BigDecimal.ONE.add(irFloatPct));
				String rateA = df.format(rateY.doubleValue());
				preListAppVO.setRealityIrY(new BigDecimal(rateA.substring(0, rateA.length()-1))); //加
				break;
			case BaseConstant.IR_FLOAT_TYPE_DOWN://基准下浮
				rateY = baseRate.multiply(BigDecimal.ONE.subtract(irFloatPct));
				String rateB = df.format(rateY.doubleValue());
				preListAppVO.setRealityIrY(new BigDecimal(rateB.substring(0, rateB.length()-1)));//减
				break;
			}	
		}
		return preListAppVO;
		
	}
	
	/**
	 * 插入是否有合同和贷款账号
	 * @param preListAppVO
	 * @param lmtPrelist
	 * @return
	 */
	public static PreListAppVO setContAndLoanAccount(
			PreListAppVO preListAppVO,
			LmtPrelist lmtPrelist,
			CtrLoanContVO ctrLoanContVO) {

		preListAppVO.setIsExistCont(ctrLoanContVO != null && ctrLoanContVO.getContNo() != null ? BaseConstant.YES : BaseConstant.NO);//是否有合同
		PreListAppListVO temp = new PreListAppListVO();
		temp.setLoanAccount(ctrLoanContVO != null && ctrLoanContVO.getLoanAccount() != null ? ctrLoanContVO.getLoanAccount() : null);//贷款账号
		preListAppVO.getList().add(temp);
		//preListAppVO.setLoanAcctNo(ctrLoanContVO!=null&&ctrLoanContVO.getLoanAccount()!=null?ctrLoanContVO.getLoanAccount():null);//贷款账号
		if (ctrLoanContVO != null && ctrLoanContVO.getLoanAccount() == null) {
			if (LimitConstant.PRELMT_STS_32.equals(lmtPrelist.getStatus())) {
				preListAppVO.setPreCreStatus(PreCreStatus.OPEN_ACCONUT_FAILED.getCode());
			}
		}
		return preListAppVO;
	}
	


}
